package org.example.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc:
 * @author: Ping
 * @date: 2023/11/13 15:25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiResponse implements Serializable {
    private Integer code;
    private String msg;
    private Map<String, Object> data;

    public static ApiResponse ok() {
        return new ApiResponse(200, "success", null);
    }

    public static ApiResponse ok(String msg) {
        return new ApiResponse(200, msg, null);
    }

    public static ApiResponse ok(Map data) {
        return ok("success", data);
    }

    public static ApiResponse ok(String key, Object value) {
        return new ApiResponse(200, "success", null).append(key, value);
    }

    public static ApiResponse ok(Object value) {
        return new ApiResponse(200, "success",null).append("参数", value);
    }

    public static ApiResponse ok(String msg, Map data) {
        return new ApiResponse(200, msg, data);
    }

    public static ApiResponse failure() {
        return new ApiResponse(400, "failure", null);
    }

    public static ApiResponse failure(String msg) {
        return new ApiResponse(400, msg, null);
    }

    public static ApiResponse failure(String key, Object value) {
        return new ApiResponse(400, "failure",null).append(key, value);
    }
    public static ApiResponse paramIncorrect(String param){
        return failure("请输入正确的"+param+"参数");
    }

    public ApiResponse append(String key, Object value) {
        if (ObjectUtils.isEmpty(this.data)) this.data = new HashMap<>();
        this.data.put(key, value);
        return ApiResponse.this;
    }
}
